package com.fick.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Query;
import org.hibernate.Session;

import com.fick.hibernate.dao.LabDAO;
import com.fick.hibernate.dao.LabinfoDAO;
import com.fick.hibernate.model.Lab;
import com.fick.hibernate.model.Labinfo;

/**
 * 根据 type字段区分 班级 教师 专业等 
 * 试验者 type=11 
 * 专业 type=22 
 * 班级 type=33 
 * 教师 type=44
 * 
 * @author Administrator
 * 
 */
public class LabInfoAction extends DispatchAction {

	private LabinfoDAO labinfodao = new LabinfoDAO();

	public ActionForward labinfoUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		if (request.getParameter("labid") != null
				&& !request.getParameter("labid").equals("")) {
			Labinfo labinfo = labinfodao.findById(Integer.parseInt(request
					.getParameter("labid")));
			String typeString = labinfo.getType();
			if (typeString.equals("11")) {

				labinfo.setStudent(request.getParameter("name"));

				labinfodao.update(labinfo);

				return mapping.findForward("labstudent_list");
			}
			if (typeString.equals("22")) {
				labinfo.setSubject(request.getParameter("name"));
				labinfodao.update(labinfo);

				return mapping.findForward("labproject_list");
			}
			if (typeString.equals("33")) {

				labinfo.setClasses(request.getParameter("name"));

				labinfodao.update(labinfo);

				return mapping.findForward("labclasses_list");
			}
			if (typeString.equals("44")) {

				labinfo.setTeacher(request.getParameter("name"));
				labinfodao.update(labinfo);
				return mapping.findForward("labteacher_list");
			}
		}

		return null;
	}

	public ActionForward labinfoAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Labinfo labinfo = new Labinfo();
		String typeString = request.getParameter("type");
		if (typeString.equals("11")) {
			labinfo.setType("11");
			labinfo.setStudent(request.getParameter("name"));
			labinfodao.getSession().beginTransaction();
			labinfodao.save(labinfo);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labstudent_list");
		}
		if (typeString.equals("22")) {
			labinfo.setType("22");
			labinfo.setSubject(request.getParameter("name"));
			labinfodao.getSession().beginTransaction();
			labinfodao.save(labinfo);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labproject_list");
		}
		if (typeString.equals("33")) {
			labinfo.setType("33");
			labinfo.setClasses(request.getParameter("name"));
			labinfodao.getSession().beginTransaction();
			labinfodao.save(labinfo);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labclasses_list");
		}
		if (typeString.equals("44")) {
			labinfo.setType("44");
			labinfo.setTeacher(request.getParameter("name"));
			labinfodao.getSession().beginTransaction();
			labinfodao.save(labinfo);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labteacher_list");
		}
		return null;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String labid = request.getParameter("labid");
		Labinfo lab = labinfodao.findById(Integer.parseInt(labid));
		String typeString=lab.getType();
		if (typeString.equals("11")) {
			labinfodao.getSession().beginTransaction();
			labinfodao.delete(lab);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labstudent_list");
		}
		if (typeString.equals("22")) {
			labinfodao.getSession().beginTransaction();
			labinfodao.delete(lab);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labproject_list");
		}
		if (typeString.equals("33")) {
			labinfodao.getSession().beginTransaction();
			labinfodao.delete(lab);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labclasses_list");
		}
		if (typeString.equals("44")) {
			labinfodao.getSession().beginTransaction();
			labinfodao.delete(lab);
			labinfodao.getSession().getTransaction().commit();
			return mapping.findForward("labteacher_list");
		}
		
		return null;
	}

}
